import { Injectable } from '@nestjs/common';

import * as AWS from 'aws-sdk';

AWS.config.update({
  region: 'us-east-1',
  accessKeyId: 'AKIAJAOIL3QW7GH5AUUA',
  secretAccessKey: '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',
});

const SES = new AWS.SES();

@Injectable()
export class SESChannelService {
  async sendEmail(subject: string, to: string, content: string): Promise<boolean> {
    return new Promise<boolean>((resolve, reject) => {
      SES.sendEmail(
        {
          Source: 'HuiRongXing <huirongxing@hrx.ai>',
          Destination: {
            ToAddresses: [to],
          },
          Message: {
            Subject: {
              Data: subject,
              Charset: 'UTF-8',
            },
            Body: {
              Text: {
                Data: content,
                Charset: 'UTF-8',
              },
              Html: {
                Data: content,
                Charset: 'UTF-8',
              },
            },
          },
        },
        (err, data) => {
          if (err) {
            console.log(err.message);
            reject(err);
          } else {
            console.log('Email sent! Message ID: ', data.MessageId);
            resolve(true);
          }
        },
      );
    });
  }
}
